package com.summli.basic.class02;

import com.sun.org.apache.xpath.internal.operations.Bool;

import java.util.LinkedList;
import java.util.Queue;

/**
 * 如何用队列结构实现栈结构
 */
public class Code07_TwoQueueImplementStack {
    public static class TwoQueueStack<T> {
        private Queue<T> queue;
        private Queue<T> help;
        public TwoQueueStack(){
            queue = new LinkedList<>();
            help = new LinkedList<>();
        }

        public void push(T value){
            queue.offer(value);
        }

        public T peek(){
            while(queue.size()>1){
                help.offer(queue.poll());
            }
            T result = queue.poll();
            help.offer(result);
            Queue<T> temp = queue;
            queue = help;
            help = temp;
            return result;
        }

        public T poll(){
            while(queue.size()>1){
                help.offer(queue.poll());
            }
            T result = queue.poll();
            Queue<T> temp = queue;
            queue = help;
            help = temp;
            return result;
        }
        public Boolean isEmpty(){
            return queue.isEmpty();
        }
    }
}
